/* @(#)GSBusOrderProcessor.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-7    建立
*   Jenny   Li   2006-2-16  添加方法getOrderAndManufacture，submitOrders,getManufactureByOrderId,
*                                 getQueryOrderSql,getOrderByOpt 
*   Road Zheng   2006-2-17  修改方法orderStatusModify， orderCancel                             
*   Jenny   Li   2006-2-21  添加方法getOrderAndOrderDetail,submitOrderDetailToApep     
*   simonyi Peng 2006-3-21  添加方法saveOrder,修改GSOrderDAO为GSBusOrderDAO变量                               
*   
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.business;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.bean.GSOrderBean;
import com.hitrust.gs.dao.GSBusOrderDAO;
import com.hitrust.gs.dao.GSOperatorDAO;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseProcesser;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderVO;

public class GSBusOrderProcessor implements GSBaseProcesser {
    
    private GSBusOrderDAO orderDAO = new GSBusOrderDAO();
    
    /**
     * 订单取消
     * @param aGSOperatorBean 操作员信息
     * @param aGSOrderBean 订单基本信息
     * @throws GSException
     */
    public void orderCancel (GSOperatorBean aGSOperatorBean,GSOrderBean aGSOrderBean) throws GSException {
    	GSBusOrderDAO tGSBusOrderDAO = new GSBusOrderDAO();
    	Connection tConn = null;
    	try {
    		
    		GSOperatorDAO tGSOperatorDAO = new GSOperatorDAO();
    		GSOperatorBean tGSOperatorBeanTemp = tGSOperatorDAO.queryOperById(aGSOrderBean.getGs_recorder_id());
    		tConn = tGSBusOrderDAO.getConnetion();
    		tConn.setAutoCommit(false);
    		boolean tResult = false; //订单取消是否成功
    		//订单归属判断
//    		if(tGSOperatorBeanTemp != null && tGSOperatorBeanTemp.getGs_oper_org_id().substring(0,2).equals("00")) { //归属巴士
//				//操作员角色判断
//				if( tGSOperatorDAO.isRecorder(aGSOperatorBean.getGs_oper_id(),"2") && aGSOperatorBean.getGs_oper_id().equals(aGSOrderBean.getGs_recoder_id())) {//录入人员
//					if(aGSOrderBean.getGs_order_status().equals("1")) { //订单状态 申请
						tGSBusOrderDAO.updateOrderStatusToCancel(aGSOrderBean.getGs_order_tempid(),"11",tConn);
						tResult = true;
//					} else {
//						throw new GSException (GSException.BME10046,GSException.BME10046_MSG);
//					}
//				} else {
//					throw new GSException (GSException.BME10047,GSException.BME10047_MSG);
//				} 
//			}  else {
//				throw new GSException (GSException.BME10442,GSException.BME10442_MSG);
//			} 
    		if(!tResult) {
    			throw new GSException (GSException.E10044,GSException.E10044_MSG);
    		} else {
    			tConn.commit();
    		}
    	} catch(SQLException e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (GSException.E19994,GSException.E19994_MSG);
    	} catch(GSException e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (e.getCode(),e.getMessage());
    	} catch(Exception e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (GSException.E10044,GSException.E10044_MSG);
    	} finally{
            GSBusOrderDAO.close(tConn);
        }
    	
    }
            
    
    /**
     * 订单状态修正 
     * @param aGSOperatorBean 操作员信息
     * @param aGSOrderBean 订单基本信息
     * @throws GSException
     */
    public void orderStatusModify (GSOperatorBean aGSOperatorBean,GSOrderBean aGSOrderBean,String aOrderStatus) throws GSException {
    	GSBusOrderDAO tGSBusOrderDAO = new GSBusOrderDAO();
    	Connection tConn = null;
    	try {
    		
    		GSOperatorDAO tGSOperatorDAO = new GSOperatorDAO();
    		GSOperatorBean tGSOperatorBeanTemp = tGSOperatorDAO.queryOperById(aGSOrderBean.getGs_recorder_id());
    		tConn = tGSBusOrderDAO.getConnetion();
    		tConn.setAutoCommit(false);
    		boolean tResult = false; //订单状态修正是否成功
    		//订单归属判断
    		if(tGSOperatorBeanTemp != null && tGSOperatorBeanTemp.getGs_oper_org_id().substring(0,2).equals("00")) { //归属巴士
				//操作员角色判断
    			if(aGSOrderBean.getGs_order_status().equals("2")) {
					if(tGSOperatorDAO.isFirstAuditor(aGSOperatorBean.getGs_oper_id(),"2")
							&& (aGSOperatorBean.getGs_oper_id().equals(aGSOrderBean.getGs_firstauditor_id()) 
											&& aGSOrderBean.getGs_order_status().equals("2"))) { //初审人员
						if(checkOrderDetailStatus(aGSOrderBean.getGs_order_tempid())) {
							tGSBusOrderDAO.updateOrderStatusToBefore(aGSOrderBean.getGs_order_tempid(),"12",aOrderStatus,tConn);
							tResult = true;
						} else {
							throw new GSException (GSException.E10449,GSException.E10449_MSG);
						}
					} else {
						throw new GSException (GSException.E10048,GSException.E10048_MSG);
					}
    			}
    			else if(aGSOrderBean.getGs_order_status().equals("3")) {
    				if(tGSOperatorDAO.isReviewer(aGSOperatorBean.getGs_oper_id(),"2")
						&& (aGSOperatorBean.getGs_oper_id().equals(aGSOrderBean.getGs_reviewer_id()) 
										&& aGSOrderBean.getGs_order_status().equals("3"))) { //复审人员
    					if(checkOrderDetailStatus(aGSOrderBean.getGs_order_tempid())) {
							tGSBusOrderDAO.updateOrderStatusToBefore(aGSOrderBean.getGs_order_tempid(),"13",aOrderStatus,tConn);
							tResult = true;
    					} else {
							throw new GSException (GSException.E10449,GSException.E10449_MSG);
						}
    				} else {
    					throw new GSException (GSException.E10049,GSException.E10049_MSG);
    				}
				} 
			} else {
				throw new GSException (GSException.E10443,GSException.E10443_MSG);
			} 
    		if(!tResult) {
    			throw new GSException (GSException.E10045,GSException.E10045_MSG);
    		} else {
    			tConn.commit();
    		}
    	} catch(SQLException e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (GSException.E19994,GSException.E19994_MSG);
    	} catch(GSException e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (e.getCode(),e.getMessage());
    	} catch(Exception e) {
    		try {
				tConn.rollback();
			} catch (SQLException e1) {
				GSLogWriter.error(this, e1.getMessage());
			}
    		GSLogWriter.error(this, e.getMessage());
    		throw new GSException (GSException.E10045,GSException.E10045_MSG);
    	} finally{
            GSBusOrderDAO.close(tConn);
        }
    }
    
    
    private boolean checkOrderDetailStatus(String aOrderTempID) 
    	throws GSException{
    	boolean tResult = false;
    	GSBusOrderDAO tGSBusOrderDAO = new GSBusOrderDAO();
    	tResult = tGSBusOrderDAO.checkOrderDetailStatus(aOrderTempID);
    	return tResult;
    }
    
	public String saveOrder(GSOperatorBean bean, GSOrderVO vo, ArrayList ary) throws GSException {

		String orderId = orderDAO.insert(bean, vo, ary);
		return orderId;
	}
}
